//! Token定义和相关类型

/// SysY语言的词法单元
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
    // 关键字
    Const, Int, Void, If, Else, While, Break, Continue, Return,
    
    // 运算符
    Plus, Minus, Mul, Div, Mod, Assign, Eq, Neq, Lt, Gt, Le, Ge, Not, And, Or,
    
    // 分隔符
    LParen, RParen, LBrace, RBrace, LBrackt, RBrackt, Comma, Semicolon,
    
    // 标识符和常量
    Ident(String),
    IntegerConst(String), // 保持原始字符串形式以支持不同进制
}

impl std::fmt::Display for Token {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let token_name = match self {
            Token::Const => "CONST", Token::Int => "INT", Token::Void => "VOID",
            Token::If => "IF", Token::Else => "ELSE", Token::While => "WHILE",
            Token::Break => "BREAK", Token::Continue => "CONTINUE", Token::Return => "RETURN",
            Token::Plus => "PLUS", Token::Minus => "MINUS", Token::Mul => "MUL",
            Token::Div => "DIV", Token::Mod => "MOD", Token::Assign => "ASSIGN",
            Token::Eq => "EQ", Token::Neq => "NEQ", Token::Lt => "LT", Token::Gt => "GT",
            Token::Le => "LE", Token::Ge => "GE", Token::Not => "NOT", Token::And => "AND", Token::Or => "OR",
            Token::LParen => "L_PAREN", Token::RParen => "R_PAREN",
            Token::LBrace => "L_BRACE", Token::RBrace => "R_BRACE",
            Token::LBrackt => "L_BRACKT", Token::RBrackt => "R_BRACKT",
            Token::Comma => "COMMA", Token::Semicolon => "SEMICOLON",
            Token::Ident(_) => "IDENT", Token::IntegerConst(_) => "INTEGER_CONST",
        };
        write!(f, "{}", token_name)
    }
}

/// 带位置信息的Token
#[derive(Debug, Clone, PartialEq)]
pub struct TokenWithPos {
    pub token: Token,
    pub line: usize,
    pub column: usize,
    pub span: String, // 原始文本
}
